<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>按姓名查询学生 - 学生信息管理系统</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#165DFF',
                        secondary: '#0FC6C2',
                        success: '#00B42A',
                        warning: '#FF7D00',
                        danger: '#F53F3F',
                        gray: {
                            50: '#F5F7FA',
                            100: '#E5E6EB',
                            200: '#C9CDD4',
                            300: '#86909C',
                            400: '#4E5969',
                            500: '#272E3B',
                            600: '#1D2129',
                        }
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                }
            }
        }
    </script>
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .card-shadow {
                box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            }
            .btn-hover {
                @apply transition-all duration-300 hover:shadow-lg transform hover:-translate-y-0.5;
            }
            .form-focus {
                @apply focus:ring-2 focus:ring-primary/30 focus:border-primary focus:outline-none;
            }
            .table-hover {
                @apply hover:bg-gray-50 transition-colors;
            }
            .search-input {
                @apply w-full pl-10 pr-4 py-2.5 border border-gray-200 rounded-lg form-focus transition-all;
            }
            .search-input:focus {
                border-color: #165DFF;
            }
        }
    </style>
</head>
<body class="bg-gray-50 font-sans min-h-screen">
    <!-- 导航栏 -->
    <header class="bg-white shadow-sm sticky top-0 z-50">
        <div class="container mx-auto px-4 py-4 flex justify-between items-center">
            <div class="flex items-center space-x-3">
                <i class="fa fa-graduation-cap text-primary text-2xl"></i>
                <h1 class="text-xl font-bold text-gray-600">学生信息管理系统</h1>
            </div>
            <nav class="hidden md:flex items-center space-x-8">
                <a href="index.html" class="text-gray-400 hover:text-primary transition-colors">首页</a>
                <a href="all-students.html" class="text-gray-400 hover:text-primary transition-colors">全部学生</a>
                <a href="student-by-id.html" class="text-gray-400 hover:text-primary transition-colors">ID查询</a>
                <a href="students-by-status.html" class="text-gray-400 hover:text-primary transition-colors">状态查询</a>
                <a href="students-by-name.html" class="text-primary font-medium">姓名查询</a>
                <a href="update-student.html" class="text-gray-400 hover:text-primary transition-colors">信息更新</a>
            </nav>
            <button class="md:hidden text-gray-500" id="mobileMenuBtn">
                <i class="fa fa-bars text-xl"></i>
            </button>
        </div>
        <!-- 移动端菜单 -->
        <div id="mobileMenu" class="md:hidden hidden bg-white border-t border-gray-100">
            <div class="container mx-auto px-4 py-2 space-y-2">
                <a href="index.html" class="block py-2 text-gray-400 hover:text-primary transition-colors">首页</a>
                <a href="all-students.html" class="block py-2 text-gray-400 hover:text-primary transition-colors">全部学生</a>
                <a href="student-by-id.html" class="block py-2 text-gray-400 hover:text-primary transition-colors">ID查询</a>
                <a href="students-by-status.html" class="block py-2 text-gray-400 hover:text-primary transition-colors">状态查询</a>
                <a href="students-by-name.html" class="block py-2 text-primary font-medium">姓名查询</a>
                <a href="update-student.html" class="block py-2 text-gray-400 hover:text-primary transition-colors">信息更新</a>
            </div>
        </div>
    </header>

    <!-- 主要内容 -->
    <main class="container mx-auto px-4 py-8">
        <div class="mb-8">
            <div class="flex items-center space-x-3 mb-2">
                <a href="index.html" class="text-gray-400 hover:text-primary transition-colors">
                    <i class="fa fa-home"></i>
                </a>
                <i class="fa fa-angle-right text-gray-300 text-xs"></i>
                <span class="text-gray-400">姓名查询</span>
            </div>
            <h2 class="text-[clamp(1.5rem,3vw,2.5rem)] font-bold text-gray-600">按姓名查询学生</h2>
            <p class="text-gray-400">输入学生姓名，查询相关学生信息</p>
        </div>

        <!-- 查询方式选择 -->
        <div class="bg-white rounded-xl p-6 card-shadow mb-8">
            <h3 class="text-lg font-semibold text-gray-600 mb-4">查询方式</h3>
            <div class="flex flex-wrap gap-4">
                <div class="flex items-center">
                    <input type="radio" id="queryParamMethod" name="queryMethod" value="queryParam" checked class="w-4 h-4 text-primary border-gray-300 focus:ring-primary">
                    <label for="queryParamMethod" class="ml-2 text-gray-600">查询参数方式 (GET /getStudentsByName?name=xxx)</label>
                </div>
                <div class="flex items-center">
                    <input type="radio" id="pathVarMethod" name="queryMethod" value="pathVar" class="w-4 h-4 text-primary border-gray-300 focus:ring-primary">
                    <label for="pathVarMethod" class="ml-2 text-gray-600">路径变量方式 (GET /getStudentsByName/xxx)</label>
                </div>
            </div>
        </div>

        <!-- 查询表单 -->
        <div class="bg-white rounded-xl p-6 card-shadow mb-8">
            <h3 class="text-lg font-semibold text-gray-600 mb-4">输入查询条件</h3>
            <form id="searchForm" class="space-y-4">
                <div class="relative max-w-2xl">
                    <span class="absolute inset-y-0 left-0 pl-3 flex items-center text-gray-400">
                        <i class="fa fa-user"></i>
                    </span>
                    <input 
                        type="text" 
                        id="studentName" 
                        name="studentName" 
                        placeholder="请输入学生姓名" 
                        required
                        class="search-input"
                    >
                    <button 
                        type="submit" 
                        class="absolute right-1 top-1 bottom-1 px-4 bg-primary text-white rounded-lg font-medium flex items-center justify-center btn-hover"
                    >
                        <i class="fa fa-search mr-2"></i>查询
                    </button>
                </div>
                
                <div class="flex items-center space-x-2 text-sm text-gray-400">
                    <i class="fa fa-info-circle"></i>
                    <span>支持模糊查询，输入姓名关键字即可找到匹配的学生信息</span>
                </div>
            </form>
        </div>

        <!-- 查询结果统计 -->
        <div id="resultStats" class="bg-white rounded-xl p-6 card-shadow mb-8 hidden">
            <div class="flex flex-wrap justify-between items-center">
                <h3 class="text-lg font-semibold text-gray-600">查询结果</h3>
                <div class="text-sm text-gray-400 flex items-center space-x-6">
                    <div>
                        <span class="text-primary font-medium" id="totalCount">0</span> 条结果
                    </div>
                    <div id="searchMethodDisplay">
                        查询方式: <span class="font-medium" id="methodType">查询参数</span>
                    </div>
                </div>
            </div>
            <div class="mt-4 grid grid-cols-1 md:grid-cols-3 gap-4">
                <div class="bg-gray-50 p-4 rounded-lg">
                    <p class="text-gray-400 text-sm mb-1">正常状态</p>
                    <p class="text-2xl font-bold text-success" id="normalCount">0</p>
                </div>
                <div class="bg-gray-50 p-4 rounded-lg">
                    <p class="text-gray-400 text-sm mb-1">已删除状态</p>
                    <p class="text-2xl font-bold text-danger" id="deletedCount">0</p>
                </div>
                <div class="bg-gray-50 p-4 rounded-lg">
                    <p class="text-gray-400 text-sm mb-1">查询耗时</p>
                    <p class="text-2xl font-bold text-gray-600" id="queryTime">0</p>
                </div>
            </div>
        </div>

        <!-- 搜索结果 -->
        <div id="searchResults" class="bg-white rounded-xl p-6 card-shadow mb-8 hidden">
            <div class="overflow-x-auto">
                <table class="w-full min-w-[640px]">
                    <thead>
                        <tr class="border-b border-gray-200">
                            <th class="text-left py-3 px-4 text-sm font-semibold text-gray-600">学号</th>
                            <th class="text-left py-3 px-4 text-sm font-semibold text-gray-600">姓名</th>
                            <th class="text-left py-3 px-4 text-sm font-semibold text-gray-600">班级</th>
                            <th class="text-left py-3 px-4 text-sm font-semibold text-gray-600">专业</th>
                            <th class="text-left py-3 px-4 text-sm font-semibold text-gray-600">性别</th>
                            <th class="text-left py-3 px-4 text-sm font-semibold text-gray-600">联系方式</th>
                            <th class="text-left py-3 px-4 text-sm font-semibold text-gray-600">状态</th>
                            <th class="text-left py-3 px-4 text-sm font-semibold text-gray-600">操作</th>
                        </tr>
                    </thead>
                    <tbody id="resultsTableBody">
                        <!-- 结果将通过JS动态添加 -->
                    </tbody>
                </table>
            </div>
            
            <!-- 空结果提示 -->
            <div id="emptyResult" class="py-12 text-center hidden">
                <div class="inline-flex items-center justify-center w-16 h-16 rounded-full bg-gray-100 text-gray-400 mb-4">
                    <i class="fa fa-search text-2xl"></i>
                </div>
                <h3 class="text-lg font-semibold text-gray-600 mb-2">未找到匹配的学生信息</h3>
                <p class="text-gray-400 max-w-md mx-auto">请尝试使用其他关键字或检查输入是否正确</p>
            </div>
        </div>

        <!-- 加载提示 -->
        <div id="loadingIndicator" class="bg-white rounded-xl p-8 card-shadow text-center hidden">
            <div class="inline-block animate-spin rounded-full h-10 w-10 border-t-2 border-b-2 border-primary mb-4"></div>
            <h3 class="text-lg font-semibold text-gray-600 mb-2">正在查询</h3>
            <p class="text-gray-400">请稍候，系统正在为您查找相关学生信息...</p>
        </div>

        <!-- 错误提示 -->
        <div id="errorMessage" class="bg-white rounded-xl p-8 card-shadow text-center hidden">
            <div class="inline-flex items-center justify-center w-16 h-16 rounded-full bg-danger/10 text-danger mb-4">
                <i class="fa fa-exclamation-circle text-2xl"></i>
            </div>
            <h3 class="text-lg font-semibold text-gray-600 mb-2">查询失败</h3>
            <p class="text-gray-400 max-w-md mx-auto mb-6" id="errorDetails">系统暂时无法处理您的请求，请稍后重试</p>
            <button id="retryBtn" class="px-6 py-2.5 bg-primary text-white rounded-lg font-medium flex items-center btn-hover mx-auto">
                <i class="fa fa-refresh mr-2"></i>重新查询
            </button>
        </div>
    </main>

    <!-- 页脚 -->
    <footer class="bg-white mt-12 py-6 border-t border-gray-100">
        <div class="container mx-auto px-4 text-center text-gray-400 text-sm">
            <p>© 2023 学生信息管理系统 - 版权所有</p>
        </div>
    </footer>

    <!-- JavaScript -->
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 表单和UI元素
            const searchForm = document.getElementById('searchForm');
            const studentNameInput = document.getElementById('studentName');
            const queryParamMethodRadio = document.getElementById('queryParamMethod');
            const pathVarMethodRadio = document.getElementById('pathVarMethod');
            const methodTypeSpan = document.getElementById('methodType');
            
            // 显示区域
            const resultStats = document.getElementById('resultStats');
            const searchResults = document.getElementById('searchResults');
            const resultsTableBody = document.getElementById('resultsTableBody');
            const emptyResult = document.getElementById('emptyResult');
            const loadingIndicator = document.getElementById('loadingIndicator');
            const errorMessage = document.getElementById('errorMessage');
            const errorDetails = document.getElementById('errorDetails');
            
            // 统计数据
            const totalCountSpan = document.getElementById('totalCount');
            const normalCountSpan = document.getElementById('normalCount');
            const deletedCountSpan = document.getElementById('deletedCount');
            const queryTimeSpan = document.getElementById('queryTime');
            
            // 按钮
            const retryBtn = document.getElementById('retryBtn');
            const mobileMenuBtn = document.getElementById('mobileMenuBtn');
            const mobileMenu = document.getElementById('mobileMenu');
            
            // 移动端菜单切换
            mobileMenuBtn.addEventListener('click', function() {
                mobileMenu.classList.toggle('hidden');
            });
            
            // 查询方式切换
            queryParamMethodRadio.addEventListener('change', function() {
                methodTypeSpan.textContent = '查询参数';
            });
            
            pathVarMethodRadio.addEventListener('change', function() {
                methodTypeSpan.textContent = '路径变量';
            });
            
            // 表单提交
            searchForm.addEventListener('submit', function(e) {
                e.preventDefault();
                const studentName = studentNameInput.value.trim();
                
                if (!studentName) {
                    showError('请输入学生姓名');
                    return;
                }
                
                searchStudentsByName(studentName);
            });
            
            // 重试按钮
            retryBtn.addEventListener('click', function() {
                errorMessage.classList.add('hidden');
                const studentName = studentNameInput.value.trim();
                if (studentName) {
                    searchStudentsByName(studentName);
                }
            });
            
            // 显示加载状态
            function showLoading() {
                resultStats.classList.add('hidden');
                searchResults.classList.add('hidden');
                errorMessage.classList.add('hidden');
                loadingIndicator.classList.remove('hidden');
            }
            
            // 显示错误
            function showError(message) {
                resultStats.classList.add('hidden');
                searchResults.classList.add('hidden');
                loadingIndicator.classList.add('hidden');
                errorDetails.textContent = message;
                errorMessage.classList.remove('hidden');
            }
            
            // 显示结果
            function showResults(students, queryTimeMs) {
                // 更新统计信息
                const totalCount = students.length;
                const normalCount = students.filter(s => s.status === 1 || s.status === '1').length;
                const deletedCount = totalCount - normalCount;
                
                totalCountSpan.textContent = totalCount;
                normalCountSpan.textContent = normalCount;
                deletedCountSpan.textContent = deletedCount;
                queryTimeSpan.textContent = `${queryTimeMs}ms`;
                
                // 更新表格内容
                resultsTableBody.innerHTML = '';
                
                if (totalCount === 0) {
                    emptyResult.classList.remove('hidden');
                } else {
                    emptyResult.classList.add('hidden');
                    
                    students.forEach(student => {
                        const row = document.createElement('tr');
                        row.className = 'border-b border-gray-100 table-hover';
                        
                        const phoneDisplay = student.phone || '-';
                        const emailDisplay = student.email || '-';
                        const contactDisplay = `${phoneDisplay}${phoneDisplay && emailDisplay ? ' / ' : ''}${emailDisplay}`;
                        
                        const statusText = student.status === 1 || student.status === '1' ? 
                            '<span class="px-2 py-1 bg-success/10 text-success rounded text-xs">正常</span>' : 
                            '<span class="px-2 py-1 bg-danger/10 text-danger rounded text-xs">已删除</span>';
                        
                        row.innerHTML = `
                            <td class="py-3 px-4 text-gray-600">${student.studentId || '-'}</td>
                            <td class="py-3 px-4 font-medium text-gray-600">${student.name || '-'}</td>
                            <td class="py-3 px-4 text-gray-600">${student.className || '-'}</td>
                            <td class="py-3 px-4 text-gray-600">${student.major || '-'}</td>
                            <td class="py-3 px-4 text-gray-600">${student.gender || '-'}</td>
                            <td class="py-3 px-4 text-gray-600 text-sm">${contactDisplay}</td>
                            <td class="py-3 px-4">${statusText}</td>
                            <td class="py-3 px-4">
                                <div class="flex space-x-2">
                                    <a href="student-by-id.html?id=${student.studentId}" class="px-2 py-1 bg-primary/10 text-primary rounded hover:bg-primary/20 transition-colors text-sm">
                                        <i class="fa fa-eye mr-1"></i>查看
                                    </a>
                                    <a href="update-student.html?id=${student.studentId}" class="px-2 py-1 bg-gray-100 text-gray-600 rounded hover:bg-gray-200 transition-colors text-sm">
                                        <i class="fa fa-edit mr-1"></i>编辑
                                    </a>
                                </div>
                            </td>
                        `;
                        
                        resultsTableBody.appendChild(row);
                    });
                }
                
                // 显示结果区域
                loadingIndicator.classList.add('hidden');
                errorMessage.classList.add('hidden');
                resultStats.classList.remove('hidden');
                searchResults.classList.remove('hidden');
                
                // 平滑滚动到结果区域
                resultStats.scrollIntoView({ behavior: 'smooth', block: 'start' });
            }
            
            // 查询学生信息
            function searchStudentsByName(studentName) {
                showLoading();
                const startTime = performance.now();
                let endpoint = '';
                
                // 根据选择的查询方式构建URL
                if (queryParamMethodRadio.checked) {
                    // 查询参数方式
                    endpoint = `/getStudentsByName?name=${encodeURIComponent(studentName)}`;
                } else {
                    // 路径变量方式
                    endpoint = `/getStudentsByName/${encodeURIComponent(studentName)}`;
                }
                
                fetch(endpoint)
                    .then(response => {
                        if (!response.ok) {
                            throw new Error(`HTTP error! Status: ${response.status}`);
                        }
                        return response.json();
                    })
                    .then(data => {
                        const endTime = performance.now();
                        const queryTimeMs = Math.round(endTime - startTime);
                        
                        // 处理返回的数据
                        let students = [];
                        
                        // 检查data是否是数组，或者需要从某个属性中提取数组
                        if (Array.isArray(data)) {
                            students = data;
                        } else if (data && Array.isArray(data.data)) {
                            students = data.data;
                        } else if (data && Object.keys(data).length > 0) {
                            // 可能只返回了单个学生对象，包装成数组
                            students = [data];
                        }
                        
                        showResults(students, queryTimeMs);
                    })
                    .catch(error => {
                        console.error('查询失败:', error);
                        showError('查询失败，请稍后重试');
                    });
            }
            
            // 检查URL参数，支持从其他页面带参数跳转
            const urlParams = new URLSearchParams(window.location.search);
            const nameParam = urlParams.get('name');
            if (nameParam) {
                studentNameInput.value = nameParam;
                searchForm.dispatchEvent(new Event('submit'));
            }
            
            // 自动聚焦输入框
            studentNameInput.focus();
        });
    </script>
</body>
</html>